package models.repository;

import models.entities.CategoryEntity;
import models.exceptions.CreateEntityException;
import models.helpers.DBSession;
import models.repository.common.CategoryRepository;
import models.shop.Category;
import mybatis.mappers.CategoryMapper;
import mybatis.mappers.ProductMapper;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.Hashtable;
import java.util.List;

@Deprecated
public class CategoryRepositorySQL implements CategoryRepository {
    public int create(CategoryEntity category) throws IOException, CreateEntityException {
        SqlSession dbSession = DBSession.openSession();
        try {
            CategoryMapper categoryMapper = dbSession.getMapper(CategoryMapper.class);
            categoryMapper.create(category);
            dbSession.commit();
        } catch (Exception ex) {
            throw new CreateEntityException(MessageFormat.format("Cannot create category '{0}'; Original message: {1}", category.title, ex.getMessage()));
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
        return category.id;
    }

    public CategoryEntity getList(int id) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            CategoryMapper categoryMapper = dbSession.getMapper(CategoryMapper.class);
            List<CategoryEntity> categories = categoryMapper.getById(id);
            CategoryEntity categoryRoot = categories.get(0);
            categoryRoot.parents = categoryMapper.getCategoryParents(categoryRoot);
            categoryRoot.aliases = categoryMapper.getCategoryAliases(id);
            makeTree(categoryRoot, categories);
            return categoryRoot;
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public CategoryEntity getByTitle(String title) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            ProductMapper productMapper = dbSession.getMapper(ProductMapper.class);
            return productMapper.getCategoryByTitle(title.toUpperCase());
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public CategoryEntity get(int categoryId) throws IOException {
        SqlSession dbSession = DBSession.openSession();
        try{
            CategoryMapper categoryMapper = dbSession.getMapper(CategoryMapper.class);
            return categoryMapper.get(categoryId);
        } finally {
            if (dbSession != null){
                dbSession.close();
            }
        }
    }

    public List<Category> getListByParentId(int parentId) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    //TODO: Check categoryRoot parameter
    private void makeTree(CategoryEntity categoryRoot, List<CategoryEntity> categories) {
        Hashtable<Integer, CategoryEntity> hashtable = new Hashtable<Integer, CategoryEntity>();
        for (CategoryEntity category : categories){
            hashtable.put(category.id, category);
        }
        for (CategoryEntity category : categories) {
            CategoryEntity categoryEntity = hashtable.get(category.parentCategoryId);
            if (categoryEntity != null){
                categoryEntity.categories.add(category);
            }
        }
    }
}
